using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Logic.Models.Common;
using CoachingOrganizer.Toolkit.Collections.Helpers;

namespace CoachingOrganizer.Android.Adapters
{
    public class PeriodsAdapter : BaseAdapter
    {
        private Activity _context;
        private List<PeriodModel> _items = new List<PeriodModel>();
        private InteractionModel _model;

        public PeriodsAdapter(Activity context, InteractionModel model)
        {
            _context = context;
            _model = model;
            model.PeriodsChanged += Model_PeriodsChanged;
            model.PeriodsCollectionChanged += Model_PeriodsCollectionChanged;
            if (model.Periods != null)
                _items = model.Periods.ToList();
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return _items.Count; }
        }

        public override View GetView(int position, global::Android.Views.View convertView, global::Android.Views.ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(Resource.Layout.PeriodLayout, parent, false);
            HookModelToView(view, _items[position]);
            return view;
        }

        private void HookModelToView(View view, PeriodModel periodModel)
        {
            //view.Click += (sender, e) => _model.CurrentPeriod = periodModel;
            Button detailsButton = view.FindViewById<Button>(Resource.Id.detailsButton);
            detailsButton.Click += (sender, e) => _model.CurrentPeriod = periodModel;
            DatePicker startDatePicker = view.FindViewById<DatePicker>(Resource.Id.startDatePicker);
            startDatePicker.UpdateDate(periodModel.StartDate.Year, periodModel.StartDate.Month, periodModel.StartDate.Day);
            startDatePicker.Click += (sender, e) => periodModel.StartDate = new DateTime(startDatePicker.Year, startDatePicker.Month, startDatePicker.DayOfMonth);
            DatePicker finishDatePicker = view.FindViewById<DatePicker>(Resource.Id.finishDatePicker);
            finishDatePicker.UpdateDate(periodModel.FinishDate.Year, periodModel.FinishDate.Month, periodModel.FinishDate.Day);
            finishDatePicker.Click += (sender, e) => periodModel.FinishDate = new DateTime(finishDatePicker.Year, finishDatePicker.Month, finishDatePicker.DayOfMonth);
            Button saveButton = view.FindViewById<Button>(Resource.Id.saveButton);
            saveButton.Click += (sender, e) =>
            {
                periodModel.StartDate = new DateTime(startDatePicker.Year, startDatePicker.Month, startDatePicker.DayOfMonth);
                periodModel.FinishDate = new DateTime(finishDatePicker.Year, finishDatePicker.Month, finishDatePicker.DayOfMonth);
                periodModel.Save();
            };
            Button removeButton = view.FindViewById<Button>(Resource.Id.removeButton);
            removeButton.Click += (sender, e) => periodModel.Remove();
        }

        private void Model_PeriodsChanged(object sender, EventArgs e)
        {
            _context.RunOnUiThread(() =>
            {
                _items = _model.Periods.ToList();
                NotifyDataSetChanged();
            });
        }

        private void Model_PeriodsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _context.RunOnUiThread(() =>
            {
                ObservableCollectionHelper.ApplyChanges(e, _items);
                NotifyDataSetChanged();
            });
        }
    }
}